package com.itheima.info.manger.dao;

import com.itheima.info.manger.domain.Student;

import java.util.ArrayList;

public class StudentDao1 {
    //创建静态学生数组用于保存学生信息
     private static ArrayList<Student> student2 = new ArrayList<>();

    /*static {
        Student stu1 = new Student("001","1","1","1");
        Student stu2 = new Student("002","2","2","2");
        student2.add(stu1);
        student2.add(stu2);
    }*/

    public boolean addStudent(Student stu) {
       student2.add(stu);
       return true;
    }

    //创建findAllStudent()方法,将Student[]数组返回.
    public Student[] findAllStudent() {
        Student[] student1 = new Student[student2.size()];
        for (int i = 0; i < student1.length; i++) {
            student1[i] = new Student(student2.get(i).getId(),student2.get(i).getName(),student2.get(i).getAge(),student2.get(i).getBirthday());
        }
        return student1;
    }

    public boolean deleteStudent(String id) {
        //遍历数组中元素,判断传递来的id是否与数组中的id相同,如果相同将id对应索引下的元素变为null,并返回true;否则返回false.
        for (int i = 0; i < student2.size(); i++) {
            if (student2.get(i).getId().equals(id)){
                student2.remove(i);
                return true;
            }
        }
        return false;
    }

    public boolean updateStudent(Student students) {
        //遍历数组中的元素,判断传递来的students数组id是否与student数组中的id相同,如果相同将students赋值给对应索引的student,并返回true;否则返回false.
        for (int i = 0; i < student2.size();i++) {
            if (student2.get(i).getId().equals(students.getId())){
                student2.set(i,students);
                return true;
            }
        }
        return false;
    }
}
